package com.bnq.control.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;

import com.oray.sunlogin.manager.DesktopPluginManager;
import com.oray.sunlogin.plugin.remotedesktop.DSPSetting;
import com.oray.sunlogin.plugin.remotedesktop.RemoteDesktopJni;

public class DisplayUtils {

    /**
     * 进去沉浸式状态
     *
     * @param activity
     */
    public static void setFullScreenStatus(Activity activity) {
        activity.getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        setFullScreen(activity);
    }

    /**
     * 退出全部沉浸式状态
     *
     * @param activity
     */
    public static void quitFullScreenStatus(Activity activity) {
        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        quitFullScreen(activity);
    }


    /**
     * 设置App为全屏模式
     */
    public static void setFullScreen(Activity activity) {
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        activity.getWindow().setFormat(PixelFormat.TRANSLUCENT);
    }

    /**
     * 退出全屏
     */
    public static void quitFullScreen(Activity activity) {
        final WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        activity.getWindow().setAttributes(attrs);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

    /**
     * 获取屏幕高度
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getHeight();
    }

    /**
     * 获取屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    /**
     * dp转px
     *
     * @param dp
     * @param context
     * @return
     */
    public static int dp2px(int dp, Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                context.getResources().getDisplayMetrics());
    }

    public static int getDisplayScreenHeight(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager == null) return 720;
        DisplayMetrics outMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(outMetrics);
        int widthPixels = outMetrics.widthPixels;
        int heightPixels = outMetrics.heightPixels;
        return heightPixels;
    }

    /**
     * 获取最佳分辨率
     *
     * @param context
     * @return
     */
    public static DSPSetting dspSetting(Context context, DesktopPluginManager manager) {
        DSPSetting[] dspArray = new DSPSetting[50];
        if (null != manager) manager.enumDisplaySetting(dspArray);
        return bestRemoteResolution(dspArray, context);
    }

    /**
     * 获取屏幕最好分辨率
     *
     * @param dspArray
     * @param mActivity
     * @return
     */
    public static DSPSetting bestRemoteResolution(DSPSetting[] dspArray, Context mActivity) {
        DSPSetting bestDSPSetting = new DSPSetting();
        float thresholdValue = 0.01f;
        int maxWidth = 1500;
        int minWidth = 1000;
        DSPSetting smallestDspSetting = new DSPSetting();

        float lastDiffBetweenScreenRatios = 0.0f;
        int lastDiffBetweenScreenWidth = 0;

        boolean isFirstSuitableDSPSettingBoolean = true;
        int localScreenWidth = mActivity.getResources().getDisplayMetrics().widthPixels >= mActivity
                .getResources().getDisplayMetrics().heightPixels ? mActivity
                .getResources().getDisplayMetrics().widthPixels : mActivity
                .getResources().getDisplayMetrics().heightPixels;
        int localScreenHeight = mActivity.getResources().getDisplayMetrics().widthPixels <= mActivity
                .getResources().getDisplayMetrics().heightPixels ? mActivity
                .getResources().getDisplayMetrics().widthPixels : mActivity
                .getResources().getDisplayMetrics().heightPixels;
        float localScreenRatio = (float) localScreenWidth / (float) localScreenHeight;

        for (int i = 0; i < dspArray.length; i++) {
            DSPSetting aDSPSetting;
            aDSPSetting = dspArray[i];
            if (aDSPSetting != null) {
                float remoteScreenRatio = (float) aDSPSetting.getWidth() / (float) aDSPSetting.getHeight();
                if (localScreenWidth <= 1000) {
                    if (i == 0) {
                        smallestDspSetting = aDSPSetting;
                    } else {
                        if (aDSPSetting.getWidth() <= smallestDspSetting.getWidth())
                            smallestDspSetting = aDSPSetting;
                    }
                    if (aDSPSetting.getWidth() <= 1000) {
                        if (isFirstSuitableDSPSettingBoolean || lastDiffBetweenScreenWidth >
                                Math.abs(localScreenWidth - aDSPSetting.getWidth())) {
                            bestDSPSetting = aDSPSetting;
                            lastDiffBetweenScreenWidth = Math.abs(localScreenWidth - aDSPSetting.getWidth());
                            isFirstSuitableDSPSettingBoolean = false;
                        }
                    }
                    if (bestDSPSetting.getWidth() <= 0) bestDSPSetting = smallestDspSetting;
                } else {
                    if (i == 0) {
                        bestDSPSetting = aDSPSetting;
                        lastDiffBetweenScreenRatios = Math.abs(localScreenRatio - remoteScreenRatio);
                    } else {
                        if (aDSPSetting.getWidth() > maxWidth || aDSPSetting.getWidth() < minWidth)
                            continue;
                        if (lastDiffBetweenScreenRatios > Math.abs(localScreenRatio - remoteScreenRatio)) {
                            bestDSPSetting = aDSPSetting;
                            lastDiffBetweenScreenRatios = Math.abs(localScreenRatio - remoteScreenRatio);
                        }
                        if (Math.abs(lastDiffBetweenScreenRatios - Math.abs(localScreenRatio - remoteScreenRatio)) < thresholdValue
                                && aDSPSetting.getWidth() <= bestDSPSetting.getWidth()) {
                            bestDSPSetting = aDSPSetting;
                            lastDiffBetweenScreenRatios = Math.abs(localScreenRatio - remoteScreenRatio);
                        }
                    }
                }
            }
        }
        return bestDSPSetting;
    }
}
